package com.lanswon.icc.common.core.handle;

import com.lanswon.icc.common.core.constant.MsgTemplate;
import com.lanswon.icc.common.core.constant.ResultConstant;
import com.lanswon.icc.common.core.exception.CheckedException;
import org.apache.commons.lang.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.List;

/**
 * @Description
 * @Author zsw
 * @Date 2019/10/31 15:51
 * @Version V1.0
 **/

public class ExceptionMsg {
    /**
     * 检查方式：检查参数
     */
    public static int check_param = 0;
    /**
     * 检查方式：检查结果
     */
    public static int check_result = 1;


    /**
     * 参数为空
     * @param paramName
     * @return
     */
    public static String paramTemplateMsg(String paramName){
        return String.format(MsgTemplate.PARAM_TEMPLATE,paramName);
    }

    /**
     * 结果为空
     * @param paramName
     * @return
     */
    public static String resultTemplateMsg(String paramName){
        return String.format(MsgTemplate.RESULT_TEMPLATE,paramName);
    }




    public static <T> void checkedDataNull(T object,String paramName,int checkType){
        if(checkType == check_param){
            if(object instanceof String){
                String s = (String) object;
                if(StringUtils.isEmpty(s)){
                    throw new CheckedException(ResultConstant.EX_DATA_IS_NULL,paramTemplateMsg(paramName));
                }
            }else if(objCheckIsNull(object)){
                throw new CheckedException(ResultConstant.EX_DATA_IS_NULL,paramTemplateMsg(paramName));
            }
        }else if (checkType == check_result){
            if(object instanceof String){
                String s = (String) object;
                if(StringUtils.isEmpty(s)){
                    throw new CheckedException(ResultConstant.EX_DATA_IS_NULL,resultTemplateMsg(paramName));
                }
            }else if(object instanceof List){
                List list = (List) object;
                if(list.size() == 0){
                    throw new CheckedException(ResultConstant.EX_DATA_IS_NULL,resultTemplateMsg(paramName));
                }
            }else if(objCheckIsNull(object)){
                throw new CheckedException(ResultConstant.EX_DATA_IS_NULL,resultTemplateMsg(paramName));
            }
        }



    }


    /**
     * 判断对象是否为空，且对象的所有属性都为空
     * ps: boolean类型会有默认值false 判断结果不会为null 会影响判断结果
     *     序列化的默认值也会影响判断结果
     * @param object
     * @return
     */
    public static boolean objCheckIsNull(Object object){

        boolean flag = true; //定义返回结果，默认为true

        if(object == null ){
            return flag;
        }


        Class clazz = (Class)object.getClass(); // 得到类对象
        Field fields[] = clazz.getDeclaredFields(); // 得到所有属性

        for(Field field : fields){
            field.setAccessible(true);
            Object fieldValue = null;
            try {
                fieldValue = field.get(object); //得到属性值
                Type fieldType =field.getGenericType();//得到属性类型
                String fieldName = field.getName(); // 得到属性名
                System.out.println("属性类型："+fieldType+",属性名："+fieldName+",属性值："+fieldValue);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            if(fieldValue != null){  //只要有一个属性值不为null 就返回false 表示对象不为null
                flag = false;
                break;
            }
        }
        return flag;
    }





    /**
     * 查重 异常
     * @param paramName
     * @return
     */
    public static String duplicateTemplateMsg(String paramName){
        return String.format(MsgTemplate.DUPLICATE_TEMPLATE,paramName);
    }

    public static void checkDuplicate(int count ,String paramName){
        if(count > 0 ){
            throw new CheckedException(ResultConstant.EX_DATA_IS_DUPLICATE,duplicateTemplateMsg(paramName));
        }
    }





}